Part 1: PCA w/ penguins

penguin_pca <- penguins %>% 
  dplyr::select(body_mass_g, ends_with("_mm")) %>% 
  drop_na() %>% 
  scale() %>%  # scales variables by units so they aren't weighted in PCA
  prcomp() # runs PCA, df to list

penguin_pca$rotation
##                          PC1         PC2        PC3        PC4
## body_mass_g        0.5483502 0.084362920 -0.5966001 -0.5798821
## bill_length_mm     0.4552503 0.597031143  0.6443012 -0.1455231
## bill_depth_mm     -0.4003347 0.797766572 -0.4184272  0.1679860
## flipper_length_mm  0.5760133 0.002282201 -0.2320840  0.7837987
# Better alternative to code above, drop only observations missing values for PCA variable
penguin_complete <- penguins %>% 
  drop_na(body_mass_g, ends_with("mm"))

autoplot(penguin_pca,
         data = penguin_complete, # Couple observations to available data
         colour = 'species', # Sum of PC 1 & 2 (x/y axes %) explains ~90% of variance among 4 variables
         loadings = TRUE,
         loadings.label = TRUE) +
  theme_minimal() # Can still use ggplot themes, or in autoplot for specific loading mods
## Warning: `select_()` is deprecated as of dplyr 0.7.0.
## Please use `select()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Part 2: ggplot2 customization & reading in different file types

Read in an .xlsx file & do some wrangling

fish_noaa <- read_excel(here("data", "foss_landings.xlsx")) %>% 
  clean_names() %>% 
  mutate(across(where(is.character), tolower)) %>% # Apply mutate across columns that are character class to lowercase
  mutate(nmfs_name = stringr::str_sub(nmfs_name, end = -4)) %>% # Extract/replace substrings from character vector, subtract last 4 characters, if use new col name will create new col
  filter(confidentiality == 'public')

Make a customized graph:

fish_plot <- ggplot(data = fish_noaa,
                    aes(x = year, y = pounds)) +
  geom_line(aes(color = nmfs_name), show.legend = FALSE) +
  theme_minimal()

fish_plot
## Warning: Removed 6 row(s) containing missing values (geom_path).

ggplotly(fish_plot) #Interactive graph, maintained in knitted html

Use gghighlight to highlight certains series from spaghetti plot

ggplot(data = fish_noaa,
                    aes(x = year, y = pounds, group = nmfs_name)) +
  geom_line() + # Need to specify series (nmfs_species) either here by color or in aes
  theme_minimal() +
  gghighlight(nmfs_name == "tunas") # Highlight series
## Warning: Tried to calculate with group_by(), but the calculation failed.
## Falling back to ungrouped filter operation...
## label_key: nmfs_name
## Warning: Removed 6 row(s) containing missing values (geom_path).

ggplot(data = fish_noaa, aes(x = year, y = pounds, group = nmfs_name)) +
  geom_line(aes(colour = nmfs_name)) + # Need to specify series (nmfs_species) either here by color or in aes
  theme_minimal() +
  gghighlight(max(pounds) > 1e8) # Highlighted series, factored by color in geom_line (only applies to gghighlight specs)
## label_key: nmfs_name
## Warning: Removed 6 row(s) containing missing values (geom_path).

Read in from a URL, lubridate(), mutate(), make a graph w/ months in logical order

monroe_wt <- read_csv("https://data.bloomington.in.gov/dataset/2c81cfe3-62c2-46ed-8fcf-83c1880301d1/resource/13c8f7aa-af51-4008-80a9-56415c7c931e/download/mwtpdailyelectricitybclear.csv") %>% 
  clean_names()
## Parsed with column specification:
## cols(
##   date = col_character(),
##   kWh1 = col_double(),
##   kW1 = col_double(),
##   kWh2 = col_double(),
##   kW2 = col_double(),
##   solar_kWh = col_double(),
##   total_kWh = col_double(),
##   MG = col_double()
## )
monroe_ts <- monroe_wt %>% 
  mutate(date = mdy(date)) %>% 
  mutate(record_month = month(date)) %>% 
  mutate(month_name = month.abb[record_month]) %>% 
  mutate(month_name = fct_reorder(month_name, record_month)) # Convert character to factor & set values based on other variable

ggplot(data = monroe_ts, aes(month_name, y = total_k_wh)) +
  geom_jitter()

Part 3: Compound figures w/ patchwork

graph_a <- ggplot(data = penguins, aes(x = body_mass_g, y = flipper_length_mm)) +
                    geom_point()

graph_b <- ggplot(data = penguins, aes(x = species, y = flipper_length_mm)) +
  geom_jitter(aes(color = species), show.legend = FALSE)
                  
# Use | to put graphs side by side
# Use / to put graphs one over the other

graph_c <- (graph_a | graph_b) / fish_plot & theme_dark()

graph_c
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 2 rows containing missing values (geom_point).
## Warning: Removed 6 row(s) containing missing values (geom_path).

ggsave(here("fig", "graph_c_eth.png"), width = 5, height = 6)
## Warning: Removed 2 rows containing missing values (geom_point).
## Warning: Removed 2 rows containing missing values (geom_point).
## Warning: Removed 6 row(s) containing missing values (geom_path).